package com.wing.sell.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.common.utils.CodeUtil;
import com.wing.common.utils.JsonResult;
import com.wing.enterprisecab.feign.EnterpriseCabClient;
import com.wing.enterprisecab.order.request.InventoryOutPlanForm;
import com.wing.sell.dao.DeliveryPlanDao;
import com.wing.sell.model.entity.DeliveryPlan;
import com.wing.sell.model.request.DeliveryPlanDetailForm;
import com.wing.sell.model.request.DeliveryPlanForm;
import com.wing.sell.model.response.DeliveryPlanVO;
import com.wing.sell.service.DeliveryPlanDetailService;
import com.wing.sell.service.DeliveryPlanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 交付计划-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class DeliveryPlanServiceImpl extends ServiceImpl<DeliveryPlanDao, DeliveryPlan> implements DeliveryPlanService {

    @Autowired
    public EnterpriseCabClient enterpriseCabClient;
    @Autowired
    //public DeliveryPlanDetailDao deliveryPlanDetailDao;
    public DeliveryPlanDetailService deliveryPlanDetailService;

    @Override
    @Transactional(rollbackFor=Exception.class)
    public int add(DeliveryPlanForm deliveryPlanForm) {

        //添加交付计划
        DeliveryPlan deliveryPlan =new DeliveryPlan();
        //计划单编号
        String code = "";
        //String typeCode = "";
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        String today = format.format(new Date());//getDayMaxNo
        Integer maxCode = baseMapper.getDayMaxNo("sel_delivery_plan");
        String maxNo = CodeUtil.getCode(maxCode);
        code = today + maxNo;
        deliveryPlanForm.setCode(code);
        deliveryPlanForm.setNo(Integer.valueOf(maxNo));
        deliveryPlanForm.setDay(Integer.valueOf(today));

        CopyOptions copyOptions = CopyOptions.create()
                .setEditable(DeliveryPlan.class)
                .setIgnoreError(true)
                .setIgnoreNullValue(true);
        BeanUtil.copyProperties(deliveryPlanForm, deliveryPlan, copyOptions);
        
        if (baseMapper.insert(deliveryPlan)==0){
            return 0;
        }

        //添加交付计划明细
        DeliveryPlanDetailForm deliveryPlanDetailForm = new DeliveryPlanDetailForm();
        DeliveryPlanVO deliveryPlanVO = baseMapper.getInfoByCode(code);//根据编号获取交付计划id
        JSONArray jsonArray = JSONArray.parseArray(deliveryPlanForm.getProductJson());
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject obj = (JSONObject) jsonArray.get(i);

            String skuCode = obj.getString("skuCode");
            String productPic = obj.getString("productPic");
            String productName = obj.getString("productName");
            String skuSpecCode = obj.getString("skuSpecValueNames");
            String unitName = obj.getString("unitName");
            Long quantity = Long.parseLong(obj.getString("quantity"));
            String enterpriceCode = obj.getString("enterpriceCode");
            String barcode = obj.getString("barcode");
            String supplyPrice = obj.getString("supplyPrice");
            String marketPrice = obj.getString("marketPrice");
            String productTypeName = obj.getString("productTypeName");
            BigDecimal subtotalAmount = obj.getBigDecimal("subtotalAmount");
            Long packId = obj.getLong("packId");
            Integer packNum = obj.getInteger("packNum");

            deliveryPlanDetailForm.setCreator(deliveryPlan.getCreator());
            deliveryPlanDetailForm.setCreatorName(deliveryPlan.getCreatorName());
            deliveryPlanDetailForm.setDeliveryPlanId(deliveryPlanVO.getId());
            deliveryPlanDetailForm.setSkuCode(skuCode);
            deliveryPlanDetailForm.setSkuPic(productPic);
            deliveryPlanDetailForm.setProductName(productName);
            deliveryPlanDetailForm.setSkuSpecCode(skuSpecCode);
            deliveryPlanDetailForm.setUnitName(unitName);
            deliveryPlanDetailForm.setProductTypeName(productTypeName);
            deliveryPlanDetailForm.setQuantity(quantity);
            deliveryPlanDetailForm.setEnterpriceCode(enterpriceCode);
            deliveryPlanDetailForm.setBarcode(barcode);
            deliveryPlanDetailForm.setSupplyPrice(new BigDecimal(supplyPrice));
            deliveryPlanDetailForm.setMarketPrice(new BigDecimal(marketPrice));
            deliveryPlanDetailForm.setSubtotalAmount(subtotalAmount);
            deliveryPlanDetailForm.setPackId(packId);
            deliveryPlanDetailForm.setPackNum(packNum);

            if (deliveryPlanDetailService.add(deliveryPlanDetailForm)==0){
                return 0;
            }
        }


        //添加出库计划
        InventoryOutPlanForm inventoryOutPlanForm = new InventoryOutPlanForm();
        inventoryOutPlanForm.setOutWarehouseId(deliveryPlanForm.getOutWarehouseId());
        inventoryOutPlanForm.setInWarehouseId(null);
        inventoryOutPlanForm.setSendTime(deliveryPlanForm.getSendTime());
        inventoryOutPlanForm.setInoutTypeCode(deliveryPlanForm.getInoutTypeCode());
        inventoryOutPlanForm.setProductJson(deliveryPlanForm.getProductJson());
        inventoryOutPlanForm.setSelDeliveryId(deliveryPlanVO.getId());
        JsonResult result = enterpriseCabClient.deliveryOutPlanAdd(inventoryOutPlanForm);
        if (result.getCode()!=200){
            return 0;
        }
        return 1;
    }

    @Override
    public List<DeliveryPlanVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<DeliveryPlanVO> pageList4app(Page page, Map<String, Object> params) {
        List<DeliveryPlanVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public DeliveryPlanVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }
}
